// pages/confirmorder/chosencleaner/chosencleaner.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cleanList: [],
    ids: [],
    info: {},
    prePage: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this,
        pages = getCurrentPages(),
        prePage = pages[pages.length - 2].route
    this.setData({
      prePage: prePage
    })
    if(options.info) {
      that.setData({
        info: JSON.parse(options.info)
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
  },

  /**
   * 获取空闲保洁列表
   */
  getList: function() {
    let that = this,
        ids = [],
        i
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/v1/provider/choice-all?access_token=' + accessToken,
        data: {
          product_id: that.data.info.product.id
        },
        success: res => {
          if(res.data.status === 1) {
            for(i in res.data.data.provider) {
              ids.push(res.data.data.provider[i].id)
            }
            that.setData({
              ids: ids,
              cleanList: res.data.data.provider
            },() => {
              wx.hideLoading()
            })
          }
        }
      })
    })
  },

  selCleaner: function(e) {
    let that = this,
        myApp = that.data,
        cid = e.detail,
        k
    for(k in myApp.cleanList) {
      if (myApp.cleanList[k].id == cid) {
        that.setData({
          'info.cleaner': myApp.cleanList[k]
        })
      }
    }
    wx.navigateTo({
      url: '/' + that.data.prePage + '?info=' + JSON.stringify(that.data.info),
    })
  }
})